package com.lei.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lei.eduservice.entity.EduSubject;
import com.lei.eduservice.entity.excel.SubjectData;
import com.lei.eduservice.entity.subject.OneSubject;
import com.lei.eduservice.entity.subject.TwoSubject;
import com.lei.eduservice.listener.SubjectDataListener;
import com.lei.eduservice.mapper.EduSubjectMapper;
import com.lei.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 熊磊
 * @since 2020-06-03
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    @Override
    public HashMap<String, Integer> saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {

        try {
            SubjectDataListener subjectDataListener = new SubjectDataListener(eduSubjectService);
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,subjectDataListener).sheet().doRead();
            return subjectDataListener.map;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    //获取分类列表
    @Override
    public List<OneSubject> subjectList() {
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneEduSubjects = baseMapper.selectList(wrapperOne);

        ArrayList<OneSubject> finalSubjects = new ArrayList<>();

        //获取一级分类
        for (EduSubject oneEduSubject : oneEduSubjects) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oneEduSubject,oneSubject);
            finalSubjects.add(oneSubject);
        }



        //获取二级分类,这样全部获取出来是为了减轻数据库压力
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id",'0');
        List<EduSubject> twoEduSubjects = baseMapper.selectList(wrapperTwo);
        //将二级分类进行封装
        for (OneSubject finalSubject : finalSubjects) {
            ArrayList<TwoSubject> twoSubjects = new ArrayList<>();
            for (EduSubject twoEduSubject : twoEduSubjects) {
                if (twoEduSubject.getParentId().equals(finalSubject.getId())){ ;
                    TwoSubject twoSubject = new TwoSubject();
                    //将查询出来的二级分类的字段复制给传给前端的二级分类的对应的字段上
                    BeanUtils.copyProperties(twoEduSubject,twoSubject);
                    twoSubjects.add(twoSubject);
                }
            }
            finalSubject.setChildren(twoSubjects);
        }


        return finalSubjects;
    }
}
